//
//  MainListVC.m
//  DayDay
//
//  Created by wade on 16/5/16.
//  Copyright © 2016年 wade. All rights reserved.
//

#import "MainListVC.h"
#import "MainListCell.h"
#import "PhotoDetailView.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
@interface MainListVC ()
{
    NSMutableArray *dataArray;
    
    NSInteger pageindex;
}
@end

@implementation MainListVC

static NSString * const reuseIdentifier = @"MainListCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    pageindex = 1;
    
    dataArray = [[NSMutableArray alloc] init];
    
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self getdata];
        
    }];
    
    [self.collectionView.mj_footer beginRefreshing];
    
}


-(void)getdata
{
    
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setTimeoutInterval:30];
     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    [parameter setValue:[NSNumber numberWithInteger:pageindex] forKey:@"pageindex"];
    [parameter setValue:[NSNumber numberWithInteger:20] forKey:@"pagesize"];
    
    [manager POST:@"http://www.shgs123.com/api/getImgList.php" parameters:parameter
         progress:^(NSProgress * _Nonnull downloadProgress)
     {
         //NSLog(@"%lld", downloadProgress.totalUnitCount);
     }
        success:^(NSURLSessionDataTask *operation, id responseObject)
     {
         //NSLog(@"%@",responseObject);
         
         [dataArray addObjectsFromArray:responseObject];
         
         if([responseObject count] < 20)
         {
             pageindex = 1;
             [self.collectionView.mj_footer endRefreshingWithNoMoreData];
         }
         else
         {
             pageindex += 1;
             [self.collectionView.mj_footer endRefreshing];
         }
         
         
         [self.collectionView reloadData];
         
     }
       failure:^(NSURLSessionDataTask *operation, NSError *error)
     {
         NSLog(@"%@",error);
         
         [self.collectionView.mj_footer endRefreshing];
     }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [dataArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MainListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    NSArray *imgArray = [dataArray[indexPath.row] objectForKey:@"imglist"];
    
    if([imgArray count])
    {
        [cell.ImgView sd_setImageWithURL:[NSURL URLWithString:imgArray[0]] placeholderImage:nil];
        [cell.likeBtn setTitle:[dataArray[indexPath.row] objectForKey:@"like"] forState:UIControlStateNormal];
    }

    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((self.view.frame.size.width - 40) / 3 , (self.view.frame.size.width - 40) / 3);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    PhotoDetailView *pdv = [storyboard instantiateViewControllerWithIdentifier:@"PhotoDetailView"];
    pdv.imgList = [dataArray[indexPath.row] objectForKey:@"imglist"];
    pdv.imgid = [dataArray[indexPath.row] objectForKey:@"id"];
    [self.navigationController pushViewController:pdv animated:YES];
}

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
